package com.design.behavioral.chainOfResponsibility;

/**
 * 请假处理抽象类。使用责任链模式实现
 */
public abstract class LeaveHandler {
    protected final static int NUM_ONE = 1;
    protected final static int NUM_THREE = 3;
    protected final static int NUM_SEVEN = 7;

    // 该领导处理的请假天数区间
    private int numStart;
    private int numEnd;

    // 上级领导
    private LeaveHandler nextHandler;

    // 构造器，设置当前领导处理的请假范围
    public LeaveHandler(int numStart, int numEnd){
        this.numStart = numStart;
        this.numEnd = numEnd;
    }

    // 构造器，最大一级领导
    public LeaveHandler(int numStart){
        this.numStart = numStart;
    }

    // 设置上级领导
    public void setNextHandler(LeaveHandler nextHandler){
        this.nextHandler = nextHandler;
    }

    public final void submit(LeaveRequest leaveRequest){
        // 当前领导未初始化
        if (0 == this.numStart){
            return;
        }

        if (leaveRequest.getNum() >= this.numStart){
            this.handlerLeave(leaveRequest);

            // 如果还有上级，并且请假天数超过了当前领导的处理范围
            if (leaveRequest.getNum() >= this.numEnd && this.nextHandler != null){
                this.nextHandler.submit(leaveRequest);
            }else {
                System.out.println("流程结束");
            }
        }else {
            System.out.println("请假天数不正确！");
        }
    }

    // 请假处理
    abstract void handlerLeave(LeaveRequest leaveRequest);
}
